// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

  @property(cc.SpriteAtlas)
  ballAtlas: cc.SpriteAtlas = null


  // LIFE-CYCLE CALLBACKS:

  onLoad() {

  }

  start() {
    // let sprite = this.node.getComponent(cc.Sprite)
    // sprite.spriteFrame = this.ballAtlas.getSpriteFrame('ball1')

    // this.nextBall()
  }

  // 随机球的颜色
  randBall(typeCnt: number) {
    let frames = this.ballAtlas.getSpriteFrames()

    if (typeCnt > frames.length) {
      typeCnt = frames.length
    }

    let randIndex: number = parseInt(String(Math.random() * typeCnt))
    let sprite = this.node.getComponent(cc.Sprite)
    sprite.spriteFrame = frames[randIndex]
  }

  nextBall() {
    // 拿到所有的图集
    let frames = this.ballAtlas.getSpriteFrames()
    // 拿到sprite
    let sprite = this.node.getComponent(cc.Sprite)

    let index = + sprite.spriteFrame.name

    let nextIndex = index + 1
    if (nextIndex >= frames.length) {
      nextIndex = 0
    }
    sprite.spriteFrame = frames[nextIndex]
    console.log(index)

  }
  // update (dt) {}
}
